﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using AnyOffice.BIZ;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using Microsoft.ApplicationBlocks.Data ;


namespace AnyOffice.general.notify.manage
{
    public partial class query : ModulePage
    {
        public string GetNew(string readers)
        {
            readers = "," + readers + ",";
            if (readers.IndexOf("," + new UserCache().UserId + ",") < 0)
            {
                return "<img src=/images/new.gif align=absmiddle>";
            }
            return string.Empty;
        }
        public string GetStatusDesc(int status)
        {
            switch (status)
            {
                case 0:
                    return "<font color=\"#FF0000\"><b>待生效</font>";

                case 1:
                    return "<font color=\"#00AA00\"><b>已生效</font>";

                case 2:
                    return "<font color=\"#FF0000\"><b>已终止</font>";
            }
            return "";
        }
        public string GetUserNames(string userIds)
        {
            return StringHelper.GetUserNames(base.Conn, userIds);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.ViewState["TypeId"] = 0;
                this.ViewState["CurrentPage"] = 1;
                DataTable table = new NotifyTypeDao().SelectAll(base.Conn);
                this.sltTypeId.DataSource = table;
                this.sltTypeId.DataBind();
                this.sltTypeId.Items.Insert(0, new ListItem("全部", ""));
                this.txtSendTimeMIN.Value = string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month.ToString(), "-1" });
                this.litNavTitle.Text = "公告通知查询";
            }
            if (this.Page.IsPostBack)
            {
                this.litNavTitle.Text = "公告通知查询结果";
                this.pnlMain.Visible = false;
                this.pnlLater.Visible = true;
                ArrayList list = new ArrayList();
                StringBuilder builder = new StringBuilder();
                if (this.rblAction.SelectedValue == "query")
                {
                    builder.Append("SELECT ");
                    builder.Append("\t\t[Notify].[NotifyId], ");
                    builder.Append("\t\t[Notify].[Subject], ");
                    builder.Append("\t\tdbo.GetDeptNames(CAST([Notify].[ToId] AS nvarchar(2000))) AS ToDeptNames, ");
                    builder.Append("\t\tdbo.GetPrivNames(CAST([Notify].[ToPriv] AS nvarchar(2000))) AS ToPrivNames, ");
                    builder.Append("\t\t[Notify].[ToUser], ");
                    builder.Append("\t\tdbo.GetStatus([Notify].[BeginDate],[Notify].[EndDate],GETDATE()) AS Status, ");
                    builder.Append("\t\t[Notify].[SendTime], ");
                    builder.Append("\t\t[Notify].[BeginDate], ");
                    builder.Append("\t\t[Notify].[EndDate], ");
                    builder.Append("\t\t[Notify].[Important], ");
                    builder.Append("\t\t[Notify].[Readers], ");
                    builder.Append("\t\t[Notify_Type].[TypeName], ");
                    builder.Append("\t\t[Users].[UserName], ");
                    builder.Append("\t\t[Department].[DeptName] ");
                    builder.Append("\tFROM ");
                    builder.Append("\t\t[dbo].[Notify], ");
                    builder.Append("\t\t[dbo].[Notify_Type], ");
                    builder.Append("\t\t[dbo].[Users], ");
                    builder.Append("\t\t[dbo].[Department] ");
                    builder.Append("\tWHERE ");
                    builder.Append("\t\t[Notify].[TypeId] = [Notify_Type].[TypeId] ");
                }
                if (this.txtSubject.Value != string.Empty)
                {
                    if (this.rblAction.SelectedValue == "query")
                    {
                        builder.Append(" AND [Notify].[Subject] LIKE '%' + @Subject + '%' ");
                    }
                    else
                    {
                        builder.Append(" AND [Subject] LIKE '%' + @Subject + '%' ");
                    }
                    list.Add(new object[] { "@Subject", this.txtSubject.Value });
                }
                if (this.sltTypeId.Value != string.Empty)
                {
                    if (this.rblAction.SelectedValue == "query")
                    {
                        builder.Append(" AND [Notify].[TypeId] =  @TypeId ");
                    }
                    else
                    {
                        builder.Append(" AND [TypeId] =  @TypeId ");
                    }
                    list.Add(new object[] { "@TypeId", Convert.ToInt32(this.sltTypeId.Value) });
                }
                if (this.UserIds.Value != string.Empty)
                {
                    if (this.rblAction.SelectedValue == "query")
                    {
                        builder.Append(" AND CHARINDEX(',' + [Notify].[FromId] + ',',',' + CAST(@UserIds AS nvarchar(4000)) + ',') > 0 ");
                    }
                    else
                    {
                        builder.Append(" AND CHARINDEX(',' + [FromId] + ',',',' + CAST(@UserIds AS nvarchar(4000)) + ',') > 0 ");
                    }
                    list.Add(new object[] { "@UserIds", this.UserIds.Value });
                }
                if (this.txtSendTimeMIN.Value != string.Empty)
                {
                    if (this.rblAction.SelectedValue == "query")
                    {
                        builder.Append(" AND [Notify].[SendTime] >= @MinDate ");
                    }
                    else
                    {
                        builder.Append(" AND [SendTime] >= @MinDate ");
                    }
                    list.Add(new object[] { "@MinDate", Convert.ToDateTime(this.txtSendTimeMIN.Value + " 00:00:00") });
                }
                if (this.txtSendTimeMAX.Value != string.Empty)
                {
                    if (this.rblAction.SelectedValue == "query")
                    {
                        builder.Append(" AND [Notify].[SendTime] <= @MaxDate ");
                    }
                    else
                    {
                        builder.Append(" AND [SendTime] <= @MaxDate ");
                    }
                    list.Add(new object[] { "@MaxDate", Convert.ToDateTime(this.txtSendTimeMAX.Value + " 23:59:59") });
                }
                if (this.txtContent.Value != string.Empty)
                {
                    if (this.rblAction.SelectedValue == "query")
                    {
                        builder.Append(" AND [Notify].[Content] LIKE '%' + @Content + '%' ");
                    }
                    else
                    {
                        builder.Append(" AND [Content] LIKE '%' + @Content + '%' ");
                    }
                    list.Add(new object[] { "@Content", this.txtContent.Value });
                }
                if (this.rblAction.SelectedValue == "query")
                {
                    builder.Append("\t\tAND [Notify].[FromId] = [Users].[UserId] ");
                    builder.Append("\t\tAND [Users].[DeptId] = [Department].[DeptId] ");
                    builder.Append("\tORDER BY ");
                    builder.Append("\t\t[Notify].[Important] DESC, ");
                    builder.Append("\t\t[Notify].[NotifyId] DESC ");
                }
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray8 = (object[])list[i];
                    commandParameters[i] = new SqlParameter(objArray8[0].ToString(), objArray8[1]);
                }
                if (this.rblAction.SelectedValue == "query")
                {
                    DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , builder.ToString(), commandParameters).Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        this.rptNotify.DataSource = table2;
                        this.rptNotify.DataBind();
                    }
                    else
                    {
                        this.litMessage.Text = base.ShowMessage("无符合条件的公告通知");
                    }
                }
                else
                {
                    StringBuilder builder2 = new StringBuilder();
                    builder2.Append("UPDATE ");
                    builder2.Append("\t[dbo].[Attachment] ");
                    builder2.Append("SET ");
                    builder2.Append("\t[DeleteFlag] = 1 ");
                    builder2.Append("WHERE ");
                    builder2.Append("\t[AttachmentId] IN ");
                    builder2.Append("\t\t\t( ");
                    builder2.Append("\t\t\t\tSELECT ");
                    builder2.Append("\t\t\t\t\t[AttachmentId] ");
                    builder2.Append("\t\t\t\tFROM ");
                    builder2.Append("\t\t\t\t\t[dbo].[Notify] ");
                    builder2.Append("\t\t\t\tWHERE ");
                    builder2.Append("\t\t\t\t\tISNULL([AttachmentId],'') <> '' ");
                    builder2.Append(builder.ToString());
                    builder2.Append("\t\t\t) ");
                    builder2.Append("DELETE FROM [dbo].[Notify] WHERE 1=1 ");
                    builder2.Append(builder.ToString());
                    this.litMessage.Text = base.ShowMessage("共删除" + SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, builder2.ToString(), commandParameters).ToString() + "条公告通知");
                }
            }
        } 
    }
}
